package com.wms.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wms.entity.Resource;
import com.wms.entity.ResourceAuthority;
import com.wms.service.ResourceAuthorityService;
import com.wms.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/admin/resource")
@RestController("adminResourceController")
public class ResourceController extends BaseController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private ResourceAuthorityService resourceAuthorityService;


    @RequestMapping("/list")
    public Object list() {
        QueryWrapper<Resource> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("id", 0);
        queryWrapper.orderByAsc("orders");
        List<Resource> resources = resourceService.list(queryWrapper);
        return success(resources);
    }

    @RequestMapping("/treeList")
    public Object treeList(Long roleId) {
        List<Resource> resources = resourceService.findTree(roleId);
        return success(resources);
    }

    @RequestMapping("/save")
    public Object save(Resource resource) {
        if (StringUtils.isEmpty(resource.getName()) || resource.getType() == null || resource.getOrders() == null || resource.getParentId() == null){
            return error("参数不完整");
        }
        resourceService.save(resource);
        return success();
    }

    @RequestMapping("/update")
    public Object update(Resource resource){
        if (resource.getId() == null) return error("参数不完整");

        resourceService.updateById(resource);
        return success();
    }

    @RequestMapping("/delete")
    public Object delete(Long id){
        if (id == null) return error("参数不完整");
        resourceService.deleteById(id);
        return success();
    }

    @RequestMapping("/addAuthority")
    public Object addAuthority(Long id, Long authorityId){
        if (id == null || authorityId == null){
            return error("参数不完整");
        }

        ResourceAuthority resourceAuthority = new ResourceAuthority();
        resourceAuthority.setResourceId(id);
        resourceAuthority.setAuthorityId(authorityId);

        Boolean exist = resourceAuthorityService.exist(resourceAuthority);
        if (!exist){
            resourceAuthorityService.save(resourceAuthority);
        }
        return success();
    }

    @RequestMapping("/delAuthority")
    public Object delAuthority(Long id, Long authorityId){
        if (id == null || authorityId == null){
            return error("参数不完整");
        }

        ResourceAuthority resourceAuthority = new ResourceAuthority();
        resourceAuthority.setResourceId(id);
        resourceAuthority.setAuthorityId(authorityId);
        resourceAuthorityService.delete(resourceAuthority);
        return success();
    }

}
